package com.demo.web;

import com.demo.pojo.User;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

//@Controller
//@ResponseBody
@RestController
@RequestMapping("/json")
public class RespController2 {
    @RequestMapping("/obj")
    //@ResponseBody // 将响应的数据转成json格式
    public User index() {
        System.out.println("obj");
        User user = new User();
        user.setId(1);
        user.setName("张三");
        user.setPassword("123456");
        user.setSex("男");
        return user;
    }

    @RequestMapping("/list")
    //@ResponseBody  // 将响应的数据转成json格式
    public List<User> list() {
        System.out.println("list");
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(i);
            user.setName("张三" + i);
            user.setPassword("123456");
            user.setSex(i % 2 == 0 ? "男" : "女");
            list.add(user);
        }
        return list;
    }

    /**
     * 只接收请求，不给客户端响应数据
     */
    @RequestMapping("/nodata")
    @ResponseStatus(code = HttpStatus.NO_CONTENT)
    public void nodata(){
        System.out.println("nodata");
    }

    /**
     * 演示请求重定向
     */
    @RequestMapping("/redirect")
    public String redirect(){
        System.out.println("redirect");
        return "redirect:/json/list";
    }

}
